<?php
namespace app\models;

use Yii;

class Error extends \yii\db\ActiveRecord
{
    
    public static function tableName()
    {
        return '{{%error}}';
    }

    public function rules()
    {
        return [
            ['status_code', 'default', 'value' => ''],
            ['status_code', 'string', 'message' => 'status_code必须是字符串', 
                            'max' => 16, 'tooLong' => 'status_code最长4位'],
            ['obj_class', 'default', 'value' => ''],
            ['obj_class', 'string', 'message' => 'obj_class必须是字符串', 
                          'max' => 256, 'tooLong' => 'obj_class最长256位'],
            ['message', 'default', 'value' => ''],
            ['message', 'string', 'message' => 'message必须是字符串'],
            ['file', 'default', 'value' => ''],
            ['file', 'string', 'message' => 'file必须是字符串', 
                     'max' => 256, 'tooLong' => 'file最长256位'],
            ['line', 'default', 'value' => 0],
            ['line', 'integer', 'message' => 'line必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'line最小存储0', 
                     'max'=>4294967295, 'tooBig' => 'line最大存储4294967295'],
            ['url', 'default', 'value' => ''],
            ['url', 'string', 'message' => 'url必须是字符串', 
                    'max' => 256, 'tooLong' => 'url最长256位'],
            ['get_args', 'default', 'value' => ''],
            ['get_args', 'string', 'message' => 'get_args必须是字符串'],
            ['post_args', 'default', 'value' => ''],
            ['post_args', 'string', 'message' => 'post_args必须是字符串'],
            ['cookies', 'default', 'value' => ''],
            ['cookies', 'string', 'message' => 'cookies必须是字符串'],
            ['session', 'default', 'value' => ''],
            ['session', 'string', 'message' => 'session必须是字符串'],
            ['state', 'default', 'value' => 1],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2, 3, 4), 'message' => 'state值必须在1,2,3,4中才选择'],
            ['ip', 'string', 'message' => 'ip必须是字符串', 
                            'max' => 64, 'tooLong' => 'ip最长64位'], 
            ['browser', 'default', 'value' => ''],
            ['browser', 'string', 'message' => 'browser必须是字符串', 
                        'max' => 512, 'tooLong' => 'browser最长512位'],
            ['solve_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['solve_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'solve_time非yyyy-MM-dd HH:mm:ss格式'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }

    /*添加系统级别错误日志*/
    public static function add($data)
    {
        $data = array(
            "Error" => $data
        );
        // P($data);
        $error = new Error();
        if($error->load($data) and $error->validate()){
            if($error->save(false)){
                // $eId = $error->getPrimaryKey();
                // return $eId;
                return true;
            }
        }else{
            if($error->hasErrors()){
                echo 'error';
                P($error->getErrors());
            }
        }
        return false;
    }



}
